<?
header('content-type:text/html;charset=utf-8');
class Admin_Message_Controller extends Base_Controller {

	public function action_index()
	{   
        $username = Session::get('username');
        $s = Input::get('s');
        $args['u.message'] = array('like', '%'.$s.'%');
        $messages = DB::table('article_content as o')
            ->left_join('message as u', 'u.kid', '=', 'o.id')
            ->order_by('u.id','asc')
            ->where(function($query) use($args) {
            foreach($args as $column => $val)
            {
                $query->where($column, $val[0], $val[1]);
            }
            })->paginate(3);


    	// $messages = DB::table('article_content')
    	// 				->join('message','message.kid','=','article_content.id')
    	// 				->order_by('message.id','asc')
     //                    ->paginate(5);;
     //    $count = DB::table('message')->count();
     //    $all = ceil($count/5);
        
        return View::make('admin/message',array('username'=>$username,'messages'=>$messages,'s'=>$s));
	}


	public function action_messageajax()
	{
		$id = $_POST['id'];
		$flag = $_POST['flag'];
        $str = DB::table('message')->where('id','=',$id)->update(array('flag'=>$flag));
        if($str){
        	return 1;
        }else{
        	return 2;
        }    
	}


    public function action_show()
    {
        $id = $_GET['id'];
        $username = Session::get('username');
        $res = DB::table('message')->where('id','=',$id)->first();
        return View::make('admin/showmessage',array('username'=>$username,'res'=>$res));
    }


    public function action_dele()
    {
        $id = $_GET['id'];
        $username = Session::get('username');
        $res = DB::table('message')->where('id','=',$id)->delete();
        if($res){
            return View::make('jump/error2',array('msg'=>'删除成功','time'=>2,'url'=>'index'));
        }else{
            return View::make('jump/error',array('msg'=>'删除失败','time'=>3));
        }
       
    }






}